home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module10- qtzoo / completed source / mainframe.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  9.7 KB  |  323 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import java.util.Vector;
  5. import java.util.StringTokenizer;
  6.  
  7. import quicktime.QTException;
  8. import quicktime.QTSession;
  9. import quicktime.app.display.QTCanvas;
  10. import quicktime.app.anim.Compositor;
  11. import quicktime.app.image.QTTransition;
  12. import quicktime.std.movies.Atom;
  13. import quicktime.std.movies.AtomContainer;
  14. import quicktime.std.StdQTConstants;
  15. import quicktime.qd.QDDimension;
  16.  
  17. import quicktime.util.EndianOrder;
  18.  
  19.  
  20. /**
  21.  * QTZoo Module 10 - Putting it all together 
  22.  * This application requires QuickTime for Java
  23.  *
  24.  * @author Levi Brown
  25.  * @author Michael Hopkins
  26.  * @author Apple Computer, Inc.
  27.  * @version 10.0 4/10/2000
  28.  * 
  29.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  30.  *    
  31.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  32.  *                ("Apple") in consideration of your agreement to the following terms, and your
  33.  *                use, installation, modification or redistribution of this Apple software
  34.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  35.  *                please do not use, install, modify or redistribute this Apple software.
  36.  *
  37.  *                In consideration of your agreement to abide by the following terms, and subject
  38.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  39.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  40.  *                reproduce, modify and redistribute the Apple Software, with or without
  41.  *                modifications, in source and/or binary forms; provided that if you redistribute
  42.  *                the Apple Software in its entirety and without modifications, you must retain
  43.  *                this notice and the following text and disclaimers in all such redistributions of
  44.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  45.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  46.  *                Apple Software without specific prior written permission from Apple.  Except as
  47.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  48.  *                are granted by Apple herein, including but not limited to any patent rights that
  49.  *                may be infringed by your derivative works or by other works in which the Apple
  50.  *                Software may be incorporated.
  51.  *
  52.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  53.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  54.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  55.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  56.  *                COMBINATION WITH YOUR PRODUCTS.
  57.  *
  58.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  59.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  60.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  61.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  62.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  63.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  64.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65.  * 
  66.  */
  67.  
  68. public class MainFrame extends Panel    
  69. {
  70.     public static final int WIDTH  = 640;
  71.     public static final int HEIGHT = 480;
  72.  
  73.     /**
  74.      * Creates the QTCanvas, loads media, and displays window contents
  75.      */
  76.     public MainFrame( Window window, Progress progress ) 
  77.     {        
  78.         this.window = window;
  79.         this.progress = progress;
  80.         progress.updateProgress("Setting sail...");
  81.         setLayout(null);
  82.         setSize( WIDTH, HEIGHT );
  83.         
  84.         myQTCanvas = new QTCanvas( QTCanvas.kInitialSize, 0.5F, 0.5F );     // Create new QT Canvas
  85.         add( myQTCanvas );
  86.         
  87.         loadPanes( );    
  88.         
  89.         try
  90.         {
  91.             myQTCanvas.setClient (mapPane.getCompositor(), true);                //Set the canvas to display the map pane
  92.         }
  93.         catch (QTException exc)
  94.         {
  95.             exc.printStackTrace();
  96.         }                                                                    
  97.         
  98.         progress.updateProgress("Docking...");
  99.     }
  100.         
  101.     /**
  102.      * Responsible for exiting the program
  103.      * Hides the window and shuts down QTJ
  104.      */
  105.     public void handleQuit()
  106.     {
  107.         setVisible(false);
  108.         window.setVisible(false);
  109.         QTSession.close();
  110.         System.exit(0);
  111.     }
  112.     
  113.     /**
  114.      * Responds to requests to update regions of the screen
  115.      */
  116.       public void update( Graphics g )
  117.       {
  118.           paint(g);
  119.       }
  120.  
  121.     /**
  122.      * Returns the preferred size of the frame
  123.      */
  124.     public Dimension getPreferredSize()
  125.     {
  126.         return new Dimension(WIDTH, HEIGHT);
  127.     }
  128.  
  129.     
  130.     /**
  131.      * Loads the data displayed in each of the animal panes
  132.      */
  133.     public void loadPanes()
  134.     {
  135.         actionListener = new Action();
  136.  
  137.         mapPane = new MapPane( progress );
  138.         mapPane.addActionListener(actionListener);
  139.  
  140.         progress.updateProgress("Azimuth determined.");
  141.         
  142.         zebraPane = new ZebraPane( progress );
  143.         zebraPane.addActionListener(actionListener);
  144.  
  145.         progress.updateProgress("Zebras aboard.");
  146.         
  147.         giraffePane = new GiraffePane(progress);
  148.         giraffePane.addActionListener(actionListener);
  149.         progress.updateProgress("Giraffes aboard.");
  150.  
  151.         tigerPane = new TigerPane(progress);
  152.         tigerPane.addActionListener(actionListener);
  153.         progress.updateProgress("Tigers aboard.");
  154.  
  155.         rhinoPane = new RhinoPane(progress);
  156.         rhinoPane.addActionListener(actionListener);
  157.         progress.updateProgress("Rhinoceroses aboard.");
  158.  
  159.         bearPane = new BearPane(progress);
  160.         bearPane.addActionListener(actionListener);
  161.         progress.updateProgress("Kodiak loaded.");
  162.  
  163.         elephantPane = new ElephantPane(progress);
  164.         elephantPane.addActionListener(actionListener);
  165.         progress.updateProgress("Elephants settled in.");
  166.     }
  167.     
  168.     protected void transition(ZooPane fromPane, ZooPane toPane, Point effectLoc )
  169.     {
  170.         Compositor fromComp = fromPane.getCompositor();
  171.         Compositor toComp = toPane.getCompositor();
  172.         try
  173.         {
  174.             //Make sure the compositor has had a chance to drawn itself
  175.             toComp.setGWorld(quicktime.qd.QDGraphics.validScratch);
  176.             toComp.redraw(null);
  177.             
  178.             QTTransition transition = new QTTransition(new QDDimension(MainFrame.WIDTH, MainFrame.HEIGHT));
  179.             transition.setTime(1000);
  180.             transition.setSourceImage(fromComp);
  181.             transition.setDestinationImage(toComp);
  182.             transition.setProfiled(false);
  183.             myQTCanvas.setClient (transition, true);
  184.  
  185.             AtomContainer effectSample = new AtomContainer();
  186.  
  187.             if (effectLoc == null)
  188.             {
  189.                 // We are using SMPTE Effects so set the what atom to smpt
  190.                 effectSample.insertChild (Atom.kParentIsContainer, StdQTConstants.kEffectWhatAtom, 1, 0, EndianOrder.flipNativeToBigEndian32(StdQTConstants.kWipeTransitionType));        
  191.                 // We are using SMPTE effect number 74    - start at 0%, stop at 100%
  192.                 effectSample.insertChild(Atom.kParentIsContainer, StdQTConstants.kEffectWipe, 1, 0, EndianOrder.flipNativeToBigEndian32(StdQTConstants.kRandomWipeTransitionType));
  193.             }
  194.             else
  195.             {
  196.                 effectSample.insertChild(Atom.kParentIsContainer, StdQTConstants.kEffectWhatAtom, 1, 0, EndianOrder.flipNativeToBigEndian32(StdQTConstants.kExplodeTransitionType));        
  197.                 effectSample.insertChild(Atom.kParentIsContainer, StdQTConstants.kExplodeTransitionType, 1, 0, EndianOrder.flipNativeToBigEndian32(StdQTConstants.kExplodeTransitionType));
  198.             }
  199.  
  200.             transition.setEffect(effectSample);
  201.             transition.doTransition();
  202.  
  203.             myQTCanvas.setClient (toComp, true);
  204.  
  205.             toPane.start();
  206.             fromPane.stop();
  207.         }
  208.         catch (QTException exc)
  209.         {
  210.             exc.printStackTrace();
  211.         }
  212.     }
  213.     
  214.     /**
  215.      * Responds to actions targeting the MainFrame
  216.      * Responsible for handling clicks in the animal regions in the Map Pane
  217.      */
  218.     class Action implements ActionListener
  219.     {
  220.         public void actionPerformed(ActionEvent event)
  221.         {
  222.             Object source = event.getSource();
  223.             String command = event.getActionCommand();
  224.             StringTokenizer st = new StringTokenizer(command, ";");
  225.             Vector params = new Vector();
  226.             
  227.             while (st.hasMoreTokens())
  228.             {
  229.                 params.addElement(st.nextToken());
  230.             }
  231.  
  232.             //Minimalistic error checking...
  233.             if (params.size() < 3)
  234.             {
  235.                 System.err.println("Not enough parameters in command");
  236.                 return;
  237.             }
  238.             
  239.             //Parse our action command for pertinate info    
  240.             String area = (String)params.elementAt(0);
  241.             int xLoc = 0;
  242.             int yLoc = 0;
  243.             
  244.             try
  245.             {
  246.                 xLoc = Integer.parseInt((String)params.elementAt(1));
  247.             } catch (NumberFormatException exc)
  248.             {
  249.                 exc.printStackTrace();
  250.             }
  251.             try
  252.             {
  253.                 yLoc = Integer.parseInt((String)params.elementAt(2));
  254.             } catch (NumberFormatException exc)
  255.             {
  256.                 exc.printStackTrace();
  257.             }
  258.                 
  259.             //Distribute appropriately
  260.             
  261.             if (source.equals(mapPane))
  262.             {
  263.                 if (area.equals("zebra"))
  264.                 {
  265.                     zebraPane.playSound();
  266.                     transition(mapPane, zebraPane, new Point(xLoc, yLoc));
  267.                 }
  268.                 else if (area.equals("giraffe"))
  269.                 {
  270.                     giraffePane.playSound();
  271.                     transition(mapPane, giraffePane, new Point(xLoc, yLoc));
  272.                 }
  273.                 else if (area.equals("tiger"))
  274.                 {
  275.                     tigerPane.playSound();
  276.                     transition(mapPane, tigerPane, new Point(xLoc, yLoc));
  277.                 }
  278.                 else if (area.equals("rhino"))
  279.                 {
  280.                     rhinoPane.playSound();
  281.                     transition(mapPane, rhinoPane, new Point(xLoc, yLoc));
  282.                 }
  283.                 else if (area.equals("bear"))
  284.                 {
  285.                     bearPane.playSound();
  286.                     transition(mapPane, bearPane, new Point(xLoc, yLoc));
  287.                 }
  288.                 else if (area.equals("elephant"))
  289.                 {
  290.                     elephantPane.playSound();
  291.                     transition(mapPane, elephantPane, new Point(xLoc, yLoc));
  292.                 }            
  293.                 else if (area.equals("exit"))
  294.                 {
  295.                     handleQuit();
  296.                 }            
  297.         }
  298.             else if (source instanceof AnimalPane)
  299.             {
  300.                 if (area.equals("map"))
  301.                 {
  302.                     transition((AnimalPane)source, mapPane, null );
  303.                 }
  304.             }
  305.         }
  306.     }
  307.  
  308.     protected ActionListener actionListener;
  309.     protected QTCanvas myQTCanvas;
  310.     protected MapPane mapPane;
  311.     protected ZebraPane zebraPane;
  312.  
  313.     protected GiraffePane giraffePane;
  314.     protected TigerPane tigerPane;
  315.     protected RhinoPane rhinoPane;
  316.     protected BearPane bearPane;
  317.     protected ElephantPane elephantPane;    
  318.     
  319.     protected Progress progress;
  320.  
  321.     protected Window window;
  322. }
  323.